package com.sttk.fastMark.camera.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.sttk.fastMark.camera.BaseAc;
import com.sttk.fastMark.camera.Content;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.basic.utils.RxUtil;
import com.sttk.fastMark.camera.bean.ConstructionLogBean;
import com.sttk.fastMark.camera.bean.MeetingSummaryBean;
import com.sttk.fastMark.camera.bean.WorkReportBean;
import com.sttk.fastMark.camera.databinding.ActivitySpliceReportBinding;
import com.sttk.fastMark.camera.dialog.EditTemplate1Dialog;
import com.sttk.fastMark.camera.dialog.EditTemplate2Dialog;
import com.sttk.fastMark.camera.dialog.EditTemplate3Dialog;
import com.sttk.fastMark.camera.dialog.SelTemplateDialog;
import com.sttk.fastMark.camera.utils.AppSpUtils;
import com.sttk.fastMark.camera.utils.ViewToBitmapUtils;

import java.text.SimpleDateFormat;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class SpliceReportActivity extends BaseAc<ActivitySpliceReportBinding> implements View.OnClickListener {

    public static List<String> listPath;
    private int type;
    private SelTemplateDialog selTemplateDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_splice_report;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSpliceReportBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.llAddPicture.setOnClickListener(this);
        mDataBinding.llAddText.setOnClickListener(this);
        mDataBinding.tvTemplate.setOnClickListener(this);
        mDataBinding.template1.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showEditTemplate1Dialog();
            }
        });
        mDataBinding.template2.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showEditTemplate2Dialog();
            }
        });
        mDataBinding.template3.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showEditTemplate3Dialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存
            saveImg();
        } else if (id == R.id.llTemplate1) {//模板1
            showEditTemplate1Dialog();
        } else if (id == R.id.llTemplate2) {//模板2
            showEditTemplate2Dialog();
        } else if (id == R.id.llTemplate3) {//模板3
            showEditTemplate3Dialog();
        } else if (id == R.id.llAddPicture) {//添加图片
            Intent intent = new Intent(this, AddPictureActivity.class);
            startActivityForResult(intent, Content.ADD_PICTURE_FLAG);
        } else if (id == R.id.llAddText) {//添加文字
            Intent intent = new Intent(this, InputTextActivity.class);
            startActivityForResult(intent, Content.ADD_TEXT_FLAG);
        } else if (id == R.id.tvTemplate) {//模板
            showSelTemplateDialog();
        }
    }

    @Override
    protected void initData() {
        if (listPath != null && listPath.size() != 0) {
            addPicture(listPath);
        }
        setData();
    }

    private void setData() {
        mDataBinding.template1.getRoot().setVisibility(View.GONE);
        mDataBinding.template2.getRoot().setVisibility(View.GONE);
        mDataBinding.template3.getRoot().setVisibility(View.GONE);

        if (type == 0) {
            WorkReportBean workReportBean = AppSpUtils.getWorkReport();
            if (workReportBean == null) {
                workReportBean = new WorkReportBean(getString(R.string.work_report), "", "", "", "", "#D84545", true, true, true, true);
                AppSpUtils.saveWorkReport(workReportBean);
            }
            mDataBinding.template1.getRoot().setVisibility(View.VISIBLE);
            mDataBinding.template1.tvTitle.setText(workReportBean.getTitle());
            mDataBinding.template1.tvSubtitle.setText(workReportBean.getSubtitle());
            if (TextUtils.isEmpty(workReportBean.getDate())) {
                mDataBinding.template1.tvDate.setText(getString(R.string.date) + "：" + TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
            } else {
                mDataBinding.template1.tvDate.setText(getString(R.string.date) + "：" + workReportBean.getDate());
            }
            mDataBinding.template1.tvReporter.setText(getString(R.string.reporter) + "：" + workReportBean.getReporter());
            mDataBinding.template1.tvWorkSummary.setText(workReportBean.getWorkSummary());

            mDataBinding.template1.tvSubtitle.setVisibility(workReportBean.isShowSubtitle() ? View.VISIBLE : View.GONE);
            mDataBinding.template1.tvDate.setVisibility(workReportBean.isShowDate() ? View.VISIBLE : View.GONE);
            mDataBinding.template1.tvReporter.setVisibility(workReportBean.isShowReporter() ? View.VISIBLE : View.GONE);
            mDataBinding.template1.tvWorkSummary.setVisibility(workReportBean.isShowWorkSummary() ? View.VISIBLE : View.GONE);
            mDataBinding.template1.tvTitle.setTextColor(Color.parseColor(workReportBean.getThemeColor()));
            mDataBinding.template1.tvLine1.setBackgroundColor(Color.parseColor(workReportBean.getThemeColor()));
            mDataBinding.template1.tvLine2.setBackgroundColor(Color.parseColor(workReportBean.getThemeColor()));
        } else if (type == 1) {
            ConstructionLogBean constructionLogBean = AppSpUtils.getConstructionLog();
            if (constructionLogBean == null) {
                constructionLogBean = new ConstructionLogBean(getString(R.string.construction_log), "", "", "", "", "", "#4B6DFF", true, true, true, true, true);
                AppSpUtils.saveConstructionLog(constructionLogBean);
            }
            mDataBinding.template2.getRoot().setVisibility(View.VISIBLE);
            mDataBinding.template2.tvTitle.setText(constructionLogBean.getTitle());
            if (TextUtils.isEmpty(constructionLogBean.getDate())) {
                mDataBinding.template2.tvDate.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
            } else {
                mDataBinding.template2.tvDate.setText(constructionLogBean.getDate());
            }
            mDataBinding.template2.tvWeather.setText(constructionLogBean.getWeather());
            mDataBinding.template2.tvProgress.setText(constructionLogBean.getProgress());
            mDataBinding.template2.tvRemark.setText(constructionLogBean.getRemark());
            mDataBinding.template2.tvRecorder.setText(constructionLogBean.getRecorder());

            mDataBinding.template2.llDateRow.setVisibility(constructionLogBean.isShowDate() ? View.VISIBLE : View.GONE);
            mDataBinding.template2.llWeatherRow.setVisibility(constructionLogBean.isShowWeather() ? View.VISIBLE : View.GONE);
            if (!constructionLogBean.isShowDate() && !constructionLogBean.isShowWeather()) {
                mDataBinding.template2.llDateAndWeatherRow.setVisibility(View.GONE);
            } else {
                mDataBinding.template2.llDateAndWeatherRow.setVisibility(View.VISIBLE);
            }
            mDataBinding.template2.llProgressRow.setVisibility(constructionLogBean.isShowProgress() ? View.VISIBLE : View.GONE);
            mDataBinding.template2.llRemarkRow.setVisibility(constructionLogBean.isShowRemark() ? View.VISIBLE : View.GONE);
            mDataBinding.template2.llRecorderRow.setVisibility(constructionLogBean.isShowRecorder() ? View.VISIBLE : View.GONE);
            mDataBinding.template2.tvTitle.setTextColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view1.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view2.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view3.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view4.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view5.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view6.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view7.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view8.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view9.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view10.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view11.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view12.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view13.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view14.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view15.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view16.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view17.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view18.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view19.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view20.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view21.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
            mDataBinding.template2.view22.setBackgroundColor(Color.parseColor(constructionLogBean.getThemeColor()));
        } else if (type == 2) {
            MeetingSummaryBean meetingSummaryBean = AppSpUtils.getMeetingSummary();
            if (meetingSummaryBean == null) {
                meetingSummaryBean = new MeetingSummaryBean(getString(R.string.meeting_summary), "", "", "", "", "", "", "", "#FF29FD", true, true, true, true, true, true, true);
                AppSpUtils.saveMeetingSummary(meetingSummaryBean);
            }
            mDataBinding.template3.getRoot().setVisibility(View.VISIBLE);
            mDataBinding.template3.tvTitle.setText(meetingSummaryBean.getTitle());
            mDataBinding.template3.tvSubtitle.setText(meetingSummaryBean.getSubtitle());
            if (TextUtils.isEmpty(meetingSummaryBean.getDate())) {
                mDataBinding.template3.tvDate.setText(getString(R.string.date) + "：" + TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
            } else {
                mDataBinding.template3.tvDate.setText(getString(R.string.date) + "：" + meetingSummaryBean.getDate());
            }
            mDataBinding.template3.tvReporter.setText(getString(R.string.reporter) + "：" + meetingSummaryBean.getReporter());
            mDataBinding.template3.tvRecorder.setText(meetingSummaryBean.getRecorder());
            mDataBinding.template3.tvParticipants.setText(meetingSummaryBean.getParticipants());
            mDataBinding.template3.tvLocation.setText(meetingSummaryBean.getLocation());
            mDataBinding.template3.tvTopic.setText(meetingSummaryBean.getTopic());

            mDataBinding.template3.tvSubtitle.setVisibility(meetingSummaryBean.isShowSubtitle() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.tvDate.setVisibility(meetingSummaryBean.isShowDate() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.tvReporter.setVisibility(meetingSummaryBean.isShowReporter() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.llRecorderRow.setVisibility(meetingSummaryBean.isShowRecorder() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.llParticipantsRow.setVisibility(meetingSummaryBean.isShowParticipants() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.llLocationRow.setVisibility(meetingSummaryBean.isShowLocation() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.llTopicRow.setVisibility(meetingSummaryBean.isShowTopic() ? View.VISIBLE : View.GONE);
            mDataBinding.template3.tvTitle.setTextColor(Color.parseColor(meetingSummaryBean.getThemeColor()));
        }
    }

    //保存图片
    private void saveImg() {
        mDataBinding.llAddPicture.setVisibility(View.GONE);
        mDataBinding.llAddText.setVisibility(View.GONE);
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ViewToBitmapUtils.convertWideView(mDataBinding.scSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        mDataBinding.llAddPicture.setVisibility(View.VISIBLE);
                        mDataBinding.llAddText.setVisibility(View.VISIBLE);
                        ToastUtils.showShort(R.string.save_suc);
                        finish();
                    }
                });
            }
        }, 1000);
    }

    private void addPicture(List<String> list) {
        for (String path : list) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout_add_picture, null, false);
            ImageView imageView = view.findViewById(R.id.ivImg);
            Glide.with(mContext).load(path).into(imageView);
            mDataBinding.llContent.addView(view);
        }
    }

    private void addTitle(String title) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_add_title, null, false);
        TextView textView = view.findViewById(R.id.tvTitle);
        textView.setText(title);
        mDataBinding.llContent.addView(view);
    }

    private void addText(String text) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_add_text, null, false);
        TextView textView = view.findViewById(R.id.tvText);
        textView.setText(text);
        mDataBinding.llContent.addView(view);
    }

    private void showEditTemplate1Dialog() {
        EditTemplate1Dialog dialog = new EditTemplate1Dialog(mContext);
        dialog.setListener(new EditTemplate1Dialog.Ilistener() {
            @Override
            public void clickConfirm() {
                setData();
            }
        });
        dialog.show();
    }

    private void showEditTemplate2Dialog() {
        EditTemplate2Dialog dialog = new EditTemplate2Dialog(mContext);
        dialog.setListener(new EditTemplate2Dialog.Ilistener() {
            @Override
            public void clickConfirm() {
                setData();
            }
        });
        dialog.show();
    }

    private void showEditTemplate3Dialog() {
        EditTemplate3Dialog dialog = new EditTemplate3Dialog(mContext);
        dialog.setListener(new EditTemplate3Dialog.Ilistener() {
            @Override
            public void clickConfirm() {
                setData();
            }
        });
        dialog.show();
    }

    private void showSelTemplateDialog() {
        if (selTemplateDialog == null) {
            selTemplateDialog = new SelTemplateDialog(mContext);
        }
        selTemplateDialog.setListener(new SelTemplateDialog.Ilistener() {
            @Override
            public void clickItem(int pos) {
                type = pos;
                setData();
            }
        });
        selTemplateDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Content.ADD_TITLE_RESULT) {
            String text = data.getStringExtra(Content.text);
            addTitle(text);
        } else if (resultCode == Content.ADD_TEXT_RESULT) {
            String text = data.getStringExtra(Content.text);
            addText(text);
        } else if (resultCode == RESULT_OK) {
            List<String> list = data.getStringArrayListExtra(Content.list);
            addPicture(list);
        }
    }
}